const { parse } = require("../utils/file_tool");
const { exec_shell } = require("../utils/shell_tool");
const { BaseCommand } = require("./base");

class RevisionInfoCommand extends BaseCommand {
    async execute() {
        const svnInfo = await parse(
            await exec_shell({
                cmd: "svn info",
                cwd: this.selfData.src,
                capture_stdout: true,
            }),
            "yaml",
        );
        const svnLogs = await parse(
            await exec_shell({
                cmd: `svn log -c${this.selfData.revisions} ${svnInfo.URL} --xml`,
                capture_stdout: true,
                encoding: "utf8",
            }),
            "xml",
        );
        const summary = Array.prototype.slice
            .call(svnLogs.getElementsByTagName("logentry"))
            .map((logentry) => {
                return {
                    revision:
                        logentry.attributes.getNamedItem("revision").nodeValue,
                    msg: Array.prototype.slice
                        .call(logentry.getElementsByTagName("msg"))
                        .map((v) => v.textContent)
                        .join(""),
                    author: Array.prototype.slice
                        .call(logentry.getElementsByTagName("author"))
                        .map((v) => v.textContent)
                        .join(""),
                };
            });
        return summary;
    }
}

module.exports = {
    RevisionInfoCommand,
};
